package org.ycsoft.MetroidReader;

import java.util.ArrayList;
import java.util.List;


/**
 * Represents a line record in a metroid file
 * @author Unilog
 *
 */
public class LineRecord {
	private String lineName;
	private int lineId;
	private int frequency;
	
	private String terminus;
	private List<Integer> stopsList = new ArrayList<Integer>();
	
	public LineRecord(){
		
	}
	
	public void setLineName(String value){
		lineName = value;
	}
	
	public String getLineName(){
		return lineName;
	}
	
	public void setLineId(int value){
		lineId = value;
	}
	
	public void setLineId(String value)throws InvalidFileException{
		if(value == null)
			return;
		value = value.trim();
		try{
			lineId = Integer.parseInt(value);
		}catch(Exception e){
			throw new InvalidFileException("Invalid line id");
		}
	}
	
	public int getLineId(){
		return lineId;
	}
	
	
	public void setTerminus(String value){
		terminus = value;
	}
	
	public String getTerminus(){
		return terminus;
	}
	
	public void setFrequency(int value){
		frequency = value;
	}
	
	public void setFrequency(String value)throws InvalidFileException{
		if(value == null)
			return;
		value = value.trim();
		try{
			frequency = Integer.parseInt(value);
		}catch(Exception e){
			throw new InvalidFileException("Invalid line frequency");
		}
	}
	
	public int getFrequency(){
		return frequency;
	}
	
	public void addStop(int stopId){
		stopsList.add(stopId);
	}
	
	/**
	 * Build stops list from a comma separated string representation
	 * @param values
	 * @throws InvalidFileException
	 */
	public void buildList(String values) throws InvalidFileException{
		if(values == null)
			return;
		values = values.trim();
		if(values.contains(",")){
			String[] parts = values.split(",");
			for(int i=0;i<parts.length;i++){
				try{
					stopsList.add(Integer.parseInt(parts[i]));
				}catch(Exception e){
					throw new InvalidFileException("Invalid stops list");
				}
			}
		}
	}
	
	/**
	 * @return The list of stops in this line
	 */
	public List<Integer> getStopsList(){
		return stopsList;
	}
	
	public String toString(){
		return lineName+":"+terminus;
		
	}
}
